{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 模型加载"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The shape of BiLstm Layer output: (?, 2048)\n",
      "The shape of Output Layer: (?, 200, 84)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Python\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\ops\\gradients_impl.py:96: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.\n",
      "  \"Converting sparse IndexedSlices to a dense Tensor of unknown shape. \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from D:/workspace/trsnlp/checkpoints/cws_pos_1107\\_plus-8\n"
     ]
    }
   ],
   "source": [
    "from cws.segmenter import BiLSTMSegmenter\n",
    "segmenter = BiLSTMSegmenter(data_path='D:/workspace/trsnlp/data/cws_pos/cws_pos_dict.pkl', model_path='D:/workspace/trsnlp/checkpoints/cws_pos_1107')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 模型预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2003年10月15日/TIME ，/w 杨利伟/PER 乘/v 由/p 长征二号F火箭/nz 运载/v 的/u 神舟五号飞船/nz 首次/m 进入/v 太空/s ，/w 象征/v 着/u 中国/LOC 太空/n 事业/n 向前/d 迈进/v 一大步/m ，/w 起/v 到/v 了/u 里程碑/n 的/u 作用/n 。/w '"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 示例1\n",
    "segmenter.predict('2003年10月15日，杨利伟乘由长征二号F火箭运载的神舟五号飞船首次进入太空，象征着中国太空事业向前迈进一大步，起到了里程碑的作用。')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 示例2、3说明“记录”一词在不同环境下为不同的词性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "示例2： 我/r 和/c 林长开/PER 的/u 通话/vn 记录/vn \n",
      "示例3： 记录/v 一段/m 文字/n \n"
     ]
    }
   ],
   "source": [
    "# 示例2\n",
    "print('示例2：',segmenter.predict('我和林长开的通话记录'))\n",
    "# 示例3\n",
    "print('示例3：',segmenter.predict('记录一段文字'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 示例4、5说明模型对罕见人名、少数名族姓名识别能力"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "示例4： 车雨菲/PER 在/p 挣扎/vn 中/f 被/p 对方/n 用/p 锐器/n 划伤/v 颈部/n \n",
      "示例5： 2002年5月23日下午/TIME ，/w 胡志勇/PER 在/p 新市镇/LOC 新市街/LOC 遇到/v 吉和日布/PER 等人/n \n"
     ]
    }
   ],
   "source": [
    "# 示例4\n",
    "print('示例4：',segmenter.predict('车雨菲在挣扎中被对方用锐器划伤颈部'))\n",
    "# 示例5\n",
    "# 对比Hanlp(http://hanlp.com/)“吉和日布”识别错误\n",
    "print('示例5：',segmenter.predict('2002年5月23日下午，胡志勇在新市镇新市街遇到吉和日布等人'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 示例6新字（词）识别能力"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "示例6： 漹城镇/LOC \n"
     ]
    }
   ],
   "source": [
    "# 示例6\n",
    "# “漹”字未出现在训练集\n",
    "print('示例6：',segmenter.predict('漹城镇'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
